<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <script type="text/javascript" src="jquery-1.4.4.min.js"></script>
 <script type="text/javascript" src="ajaxfileupload.js"></script> 
<title></title>
<script type="text/javascript">  
$(function(){  
	$("#btnupload").click(function(){ 
		$("#fileToUpload").click();
	});
	
    //选择文件之后执行上传  
    $("#fileToUpload").change(function(){ 
        $.ajaxFileUpload({  
            url:'fileuploadservlet', 
            type: "post",   
            secureuri:false,  
            fileElementId:'fileToUpload',//file标签的id  
            dataType: 'json',//返回数据的类型  
            data:{"titleid":5},//一同上传的数据  
            success: function (data) {
                //把图片替换  
               var data1 = data.result.value;
               var obj = eval(data.result); //eval是将字符串转换为对象

                $("#upload").attr("src", "upload/"+obj.fileName);  
      			$("#txtuploadpath").val(obj.fileName);
                if(typeof(data.error) != 'undefined') {  
                    if(data.error != '') {  
                        alert(data.error);  
                    } else {  
                        alert(data.msg);  
                    }  
                }  
            },  
            error: function (data, status, e) {  
                alert(e);  
            }  
        });  
    });  
      
});  
</script>  

</head>

<body>
<!-- 点击图片，打开文件选择器，确定，上传。(这是网络上的一个图片) -->  
<img id="upload" alt="" style="width: 200px; height: 200px"  
    src="http://d.pcs.baidu.com/thumbnail/e8119cd92364a9b2714ea0a92af15aec?fid=2399642819-250528-305026848845811&time=1405674000&sign=FDTAER-DCb740ccc5511e5e8fedcff06b081203-abo3xnZkLb7yMEPLDWiuaQI8kXM%3D&rt=sh&expires=2h&r=900585425&sharesign=unknown&size=c710_u500&quality=100"/>  
图片名称：<input id="txtuploadpath" type="text" size="20" />
<input id="btnupload" type="button" name = "btnupload" value="载入并上传图片"/>  
<!-- 隐藏file标签 -->  
<input id="fileToUpload" style="display:none" type="file" name="upfile"/><br/> 
</body>
</html>
